cc.Class({
    extends: cc.Component,
	
	getMapPointByPosition(tx,ty,game){
		for(var k=0;k<game.mapPointArr.length;k++){
				var tmp = game.mapPointArr[k];
				
				if(tmp.touched(tx,ty)){
					
					return tmp;
				}
				
		}
		return null;
	},
	
	exchange(tmp,game){
		if(game.selectedBlock.isNear(tmp)){
			game.node.removeChild(game.selectedMask);
			game.selectedBlock2 = tmp;
			game.selectedBlock.exchange(tmp,game.exChangeCallback);			
		}else{	
			game.node.removeChild(game.selectedMask);
			game.selectedBlock = tmp;
			game.selectedMask = game.createNode(game.selectedBlock.x,game.selectedBlock.y,game.maskFrame,game.mapScale);
		}
	},

    addTouchStart(game){
		game.node.on(cc.Node.EventType.TOUCH_START,function(event){
			 
			if(!game.gameManager.canTouchStart()){
				console.log('cant select .. ');
				return ;
			}
			
			var tx = event.getLocationX(),ty = event.getLocationY();
			
			var tmp = this.getMapPointByPosition(tx,ty,game);
			
			if(tmp!=null&&tmp.blockNode!=null){
				if(game.selectedBlock!=null){
					this.exchange(tmp,game);
				}else{
					game.selectedBlock = tmp;
					game.selectedMask = game.createNode(game.selectedBlock.x,game.selectedBlock.y,game.maskFrame,game.mapScale);
				}
			}

		}.bind(this));
	},
	
	addTouchMove(game){
		game.node.on(cc.Node.EventType.TOUCH_MOVE,function(event){
				
			if(!game.gameManager.canTouchMove()){
				console.log('cant move .. ');
				return ;
			}
			
			var tx = event.getLocationX(),ty = event.getLocationY();
			
			var tmp = this.getMapPointByPosition(tx,ty,game);
			
			if(tmp!=null&&tmp.blockNode!=null){
				this.exchange(tmp,game);
			}
			
		
		}.bind(this));
	},
});
